/**
 * Created by Administrator on 2018/5/10.
 */
define(function (require,exports,module) {
    require("./enter.css");
    require("../../../lib/fullcalendar/moment.min.js");
    var sTpl = require("./template.html");
    var VueComponent = Vue.extend({
        props: ['parentData'],
        template: sTpl,
        data:function () {
            return {
                formInline: {
                    user: '',
                    region: '',
                    flightInfo:{flightType:'',flightInfoId:'',flightNum:''},
                    flightType:'',
                    flightNum:'',
                    expectDate:'',
                    importOrExport:'',
                    originating:'',
                    destination:'',
                    takeOffTime:'',
                    arriveTime:'',
                    preFlyTime:'',
                    preArriveTime:'',
                    planeStatus:'新建'

                },
                type:"normal",
                planes:[],
                planesMap:{},
                fileInputDestroy:false,
                excelDate:[],
                excelView:false,
                xlf:{},
                xlfName:"",
                jsLoaded:false,
                citys:[{value:'北京'},
                    {value:'天津'},
                    {value:'上海'},
                    {value:'徐州'},
                    {value:'常州 '},
                    {value:'江苏'},
                    {value:'南通'},
                    {value:'无锡'},
                    {value:'南京'},
                    {value:'连云港'},
                    {value:'济南'},
                    {value:'临沂'},
                    {value:'潍坊'},
                    {value:'威海'},
                    {value:'烟台'},
                    {value:'青岛'},
                    {value:'济宁'},
                    {value:'黄岩路'},
                    {value:'衢州'},
                    {value:'舟山'},
                    {value:'宁波'},
                    {value:'温州'},
                    {value:'杭州'},
                    {value:'锦州'},
                    {value:'朝阳'},
                    {value:'沈阳'},
                    {value:'大连'},
                    {value:'赤峰'},
                    {value:'内蒙古'},
                    {value:'锡林浩特'},
                    {value:'包头'},
                    {value:'呼和浩特'},
                    {value:'乌兰浩特'},
                    {value:'海拉尔'},
                    {value:'乌海'},
                    {value:'呼伦贝尔'},
                    {value:'通辽'},
                    {value:'南平市'},
                    {value:'厦门'},
                    {value:'泉州'},
                    {value:'晋江'},
                    {value:'福州'},
                    {value:'黄山屯溪'},
                    {value:'安庆'},
                    {value:'合肥'},
                    {value:'阜阳'},
                    {value:'蚌埠'},
                    {value:'嘉峪关'},
                    {value:'酒泉'},
                    {value:'庆阳'},
                    {value:'兰州'},
                    {value:'深圳'},
                    {value:'珠海'},
                    {value:'梅县'},
                    {value:'湛江'},
                    {value:'汕头'},
                    {value:'北海福城'},
                    {value:'广西'},
                    {value:'梧州'},
                    {value:'柳州'},
                    {value:'南宁'},
                    {value:'桂林'},
                    {value:'贵州'},
                    {value:'兴义'},
                    {value:'铜仁'},
                    {value:'黎平'},
                    {value:'龙洞堡'},
                    {value:'荔波'},
                    {value:'海口'},
                    {value:'三亚'},
                    {value:'秦皇岛'},
                    {value:'邯郸'},
                    {value:'邢台'},
                    {value:'石家庄'},
                    {value:'洛阳'},
                    {value:'南阳'},
                    {value:'郑州'},
                    {value:'黑河'},
                    {value:'佳木斯'},
                    {value:'哈尔滨'},
                    {value:'齐齐哈尔'},
                    {value:'牡丹江'},
                    {value:'武汉'},
                    {value:'宜昌'},
                    {value:'襄樊'},
                    {value:'恩施'},
                    {value:'常德'},
                    {value:'张家界'},
                    {value:'长沙'},
                    {value:'怀化'},
                    {value:'延吉'},
                    {value:'长白山'},
                    {value:'吉林'},
                    {value:'长春'},
                    {value:'德镇'},
                    {value:'井冈山'},
                    {value:'九江庐山'},
                    {value:'吉安'},
                    {value:'南昌'},
                    {value:'赣州'},
                    {value:'银川'},
                    {value:'西宁'},
                    {value:'格尔木'},
                    {value:'运城'},
                    {value:'太原'},
                    {value:'大同'},
                    {value:'长治'},
                    {value:'西安'},
                    {value:'安康'},
                    {value:'延安'},
                    {value:'榆林'},
                    {value:'汉中'},
                    {value:'成都'},
                    {value:'康定'},
                    {value:'达县'},
                    {value:'西昌'},
                    {value:'绵阳'},
                    {value:'四川攀枝花'},
                    {value:'九寨沟'},
                    {value:'广汉'},
                    {value:'南充'},
                    {value:'宜宾'},
                    {value:'林芝'},
                    {value:'昌都邦达'},
                    {value:'拉萨贡嘎'},
                    {value:'阿勒泰'},
                    {value:'伊宁'},
                    {value:'喀纳斯'},
                    {value:'喀什'},
                    {value:'库尔勒'},
                    {value:'克拉玛依'},
                    {value:'库车'},
                    {value:'且末'},
                    {value:'富蕴'},
                    {value:'和田'},
                    {value:'哈密'},
                    {value:'伊犁那拉提'},
                    {value:'鄯善'},
                    {value:'塔城'},
                    {value:'乌鲁木齐'},
                    {value:'阿克苏'},
                    {value:'景洪'},
                    {value:'昭通'},
                    {value:'文山普者黑'},
                    {value:'普洱'},
                    {value:'大理'},
                    {value:'丽江'},
                    {value:'云南迪庆香格里拉'},
                    {value:'德宏芒市'},
                    {value:'昆明巫家坝'},
                    {value:'重庆'},
                    {value:'香港'},
                    {value:'澳门'},
                    {value:'台北'},
                    {value:'桃园'},
                    {value:'台中'},
                    {value:'台东'},
                    {value:'台南'}
                ],
                flightInfo:{}
            };
        },
        methods:{
            pickExpectDate:function (val) {
                this.formInline.expectDate = val;
            },
            closeThis:function () {
              this.parentData.close();
            },
            xlFileChange:function (e) {
                this.fileInputDestroy=true;
                this.xlfName = e.target.value;
                this.xlf = e.target.files;
                this.do_file(this.xlf);
            },
            closeExcelTable:function () {
                this.excelView=false;
                this.xlfName= ""
            },
            batchSave:function () {
                var $this= this;
                this.closeExcelTable();
                axios.post(basepath+"/api/flightPlane/batchSave",$this.excelDate).then(function (response) {
                    $this.$notify(
                        {title:'保存成功', message:'航班计划保存成功'});
                        $this.parentData.editCallback();
                });
            },
            batchSaveAndPublish:function () {
                this.closeExcelTable();
                var $this= this;
                $this.excelDate.forEach(function (data) {
                   data.planeStatus="发布";
                })
                axios.post(basepath+"/api/flightPlane/batchSave",$this.excelDate).then(function (response) {
                    $this.$notify(
                        {title:'保存成功', message:'航班计划保存成功'});
                         $this.parentData.editCallback();
                });
            },
            do_file:function (files) {
                if(!this.jsLoaded){
                    loadJs();
                }
                var rABS = typeof FileReader !== "undefined" && (FileReader.prototype||{}).readAsBinaryString;
                var f = files[0];
                var reader = new FileReader();
                var $this =this;
                reader.onload = function(e) {
                    try{
                        if(typeof console !== 'undefined') console.log("onload", new Date(), rABS);

                        var data = e.target.result;
                        var wb = XLS.read(data, {type: rABS ? 'binary' : 'array'});
                        if(!rABS) data = new Uint8Array(data);
                        var ws = wb.Sheets[wb.SheetNames[0]];
                        var data = XLSX.utils.sheet_to_json(ws, {header:1});



                        var a=['flightInfo','expectDate',
                            'importOrExport','originating','destination','takeOffTime','arriveTime'];
                        var b=['航班号','预约日期',
                            '进出港','出发地','目的地','计划起飞时间','计划到达时间']
                        $this.excelDate=[];
                        for(var i=1;i<data.length;i++){
                            $this.excelDate[i]={};
                            for(var n in a){
                                $this.excelDate[i][a[n]] = data[i][n];
                                if(!data[i][n]){
                                    $this.$notify({title:"excel内容异常",message:"字段 "+b[n]+" 不能为空"});
                                }
                            }
                            $this.excelDate[i]['planeStatus']="新建";
                            $this.excelDate[i].flightInfo=$this.planesMap[$this.excelDate[i].flightInfo];
                            try{
                                $this.excelDate[i].takeOffTime =  moment(new Date($this.excelDate[i].takeOffTime)).format("YYYY-MM-DD HH:mm");
                                $this.excelDate[i].arriveTime  = moment(new Date($this.excelDate[i].arriveTime)).format("YYYY-MM-DD HH:mm");
                                $this.excelDate[i].expectDate  = moment(new Date($this.excelDate[i].expectDate)).format("YYYY-MM-DD");
                            }catch(e) {
                                $this.$notify({title:"日期格式异常",message:"请检查文件内容"});
                            }
                            // $this.excelDate[i].takeOffTime=replaceAll($this.excelDate[i].takeOffTime,"/","-");
                            // $this.excelDate[i].arriveTime=replaceAll($this.excelDate[i].arriveTime,"/","-");
                        }
                        $this.excelDate.splice(0,1);
                        $this.excelView = true;
                    }catch (e){
                       $this.$notify({title:"文件格式异常",message:"请检查文件内容"});
                    }
                    $this.fileInputDestroy=false;
                };
                if(rABS) reader.readAsBinaryString(f);
                else reader.readAsArrayBuffer(f);
            },
            loadJs:function () {
                var $this = this;
                require.async("../../../lib/js-xlsx-master/shim.js",function (data) {
                    require.async("../../../lib/js-xlsx-master/xlsx.full.min.js", function (data) {
                        $this.jsLoaded=true;
                    });
                });
            },
            pickTakeOffTime:function (val) {
                this.formInline.takeOffTime = val;
            },
            pickArriveTime:function (val) {
                this.formInline.arriveTime = val;
            },
            pickPreFlyTime:function (val) {
                this.formInline.preFlyTime = val;
            },
            pickPreArriveTime:function (val) {
                this.formInline.preArriveTime = val;
            },

            handleSelect:function (item) {
                this.flightInfo =this.planes.find(function (val) {
                    return val.flightInfoId ==item;
                })
               console.log(item);
            } ,querySearchPlace:function(queryString, cb) {
                var restaurants = this.citys;
                var results = queryString ? restaurants.filter(function(restaurant){
                    return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                }) : restaurants;
                // 调用 callback 返回建议列表的数据
                cb(results);
            },
            save:function () {
                var $this = this;
                this.$refs.formInline.validate(function (validate) {
                    if(validate){
                        axios.post(basepath+"/api/flightPlane/save",$this.formInline).then(function (response) {

                            $this.$notify(
                                {title:'保存成功', message:'航班计划保存成功'});
                            if($this.parentData.editOrAdd=='edit'){
                                $this.parentData.editCallback(this.formInline);
                                $this.$refs.formInline.resetFields();
                            }else{
                                $this.$refs.formInline.resetFields();
                            }
                        });
                    }else{
                        $this.$notify({
                            title: '校验失败',
                            message: '请检查修改字段'
                        });
                    }
                })
            },
            saveAndPublish:function () {
                var $this = this;
                this.$refs.formInline.validate(function (validate) {
                    if(validate){
                        $this.formInline.planeStatus="发布"
                        axios.post(basepath+"/api/flightPlane/save",$this.formInline).then(function (response) {
                            $this.$notify(
                                {title:'保存发布成功', message:'航班计划保存发布成功'});
                            if($this.parentData.editOrAdd=='edit'){
                                $this.parentData.editCallback(this.formInline);
                            }else{
                                $this.$refs.formInline.resetFields();
                            }

                        });
                    }
                })
            }
        },
        created:function (){
            var $this=this;
            this.loadJs();
            if(this.parentData&&this.parentData.openEdit){
                this.parentData.openEdit(function (data) {
                    if($this.formInline.flightInfo.flightInfoId){
                        $this.flightInfo = $this.planes.find(function (val) {
                            return $this.formInline.flightInfo.flightInfoId ==val.flightInfoId;
                        })
                    }
                    $this.formInline = data;
                });
            }
            axios.get(basepath+"/api/flightInfo/queryFlightInfo?filter=").then(function (response) {
                $this.planes=response.data;
                $this.planesMap={};
                $this.planes.forEach(function(plane){
                    plane.value= plane.flightNum;
                    $this.planesMap[plane.flightNum] = plane;
                })
                if($this.formInline.flightInfo.flightInfoId){
                    $this.flightInfo = $this.planes.find(function (val) {
                        return $this.formInline.flightInfo.flightInfoId ==val.flightInfoId;
                    })
                }
            })

    },
        activated:function () {
            if(!this.parentData){
                messages.$emit('change','航班计划录入');
            }
        }
    })
    module.exports = VueComponent;

    return VueComponent;
});
